import { notification } from 'antd';
import { queryPage, queryInfoById, create, remove, update, queryBuildPage, queryLocationList, queryOptionList } from './service';
import { routerRedux } from 'dva/router';
import { queryDictList } from '@/services/dict'

export default {
  namespace: 'fireroom',

  state: {
    list: { data: [] },
    buildList: { data: [] },
    details: [],
    cond: {},
    buildCond: {},
    locationData: {}
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put, select }) {
      const cond = yield select(state => state.fireroom.cond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setCond', payload: params });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.fireroom.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *initBuildCond({ payload }, { put }) {
      yield put({ type: 'setBuildCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryBuildPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.fireroom.buildCond);
      const params = { ...cond, ...payload };
      if (!params.pageSize) params.pageSize = 8;
      const { list = [], total } = yield call(queryBuildPage, params);
      yield put({ type: 'setBuildPage', payload: { list, total: +total } })
    },
    *updateBuildCond({ payload }, { put, select }) {
      const cond = yield select(state => state.fireroom.buildCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setBuildCond', payload: params });
      yield put({ type: 'queryBuildPage' })
    },
    *queryLocation({ payload }, { call, put }) {
      const response = yield call(queryLocationList);
      yield put({ type: 'setLocationData', payload: response });
    },
    *optionLocation({ payload }, { call, put }) {
      const response = yield call(queryOptionList, payload);
      yield put({ type: 'setOptionData', payload: response })
    },
    *areaLocation({ payload }, { call, put }) {
      const response = yield call(queryOptionList, payload);
      yield put({ type: 'setAreaOption', payload: response });
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items = {};

      payload.keys.forEach(item => {
        items[item] = response[item]
      })
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      const data = yield call(create, payload);
      +data === 1 && notification['success']({
        message: '保存成功',
        description: '当前该消控室的管辖区域为本单位所有建筑，若需要修改请前往“编辑—管辖区域”中通过“移除或手动选择”操作更改消控室的管辖区域。',
      });
      yield put(routerRedux.goBack());
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.goBack());
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload);
      yield put({ type: 'queryPage' });
    },
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setBuildCond(state, action) {
      return {
        ...state,
        buildCond: action.payload
      }
    },
    setBuildPage(state, action) {
      return {
        ...state,
        buildList: action.payload
      }
    },
    setLocationData(state, action) {
      return {
        ...state,
        locationData: action.payload
      }
    },
    setOptionData(state, action) {
      return {
        ...state,
        optionData: action.payload
      }
    },
    setAreaOption(state, action) {
      return {
        ...state,
        areaData: action.payload
      }
    },
  }
}
